package customer.zspt.cn.customer.Border;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import java.text.SimpleDateFormat;
import java.util.Date;

import customer.zspt.cn.customer.activitys.BaseActivity;
import customer.zspt.cn.customer.InterfacePg.IGetNetState;
import customer.zspt.cn.customer.utils.NetUtils;

/**
 * 监听网络状态的广播接受者
 */

public class NetWorkStateBroder extends BroadcastReceiver {
    private IGetNetState state = BaseActivity.state;
    //切换到wifi的时间
    private long WIFITIME = 0;
    //切换到无网络的时间
    private long NOWORKTIME = 0;
    //切换到数据的时间
    private long ETHERENTTIME = 0;
    //最后时刻的网络状态
    private int Last_Time = -3;
    @Override
    public void onReceive(Context context, Intent intent) {
        //如果网络状态发生改变
        if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
            long time = getTime();
            if(time!=WIFITIME&&time!=NOWORKTIME&&time!=ETHERENTTIME){
                int netWorkState = NetUtils.getNetWorkState(context);
                if(netWorkState==NetUtils.MOBILE&&Last_Time!=NetUtils.MOBILE){
                    state.NetState(netWorkState);
                    Last_Time = netWorkState;
                    ETHERENTTIME = time;
                }else if(netWorkState==NetUtils.WIFINET&&Last_Time!=NetUtils.WIFINET){
                    state.NetState(netWorkState);
                    Last_Time = netWorkState;
                    WIFITIME = time;
                }else if(netWorkState==NetUtils.NOTNET&&Last_Time!=NetUtils.NOTNET){
                    state.NetState(netWorkState);
                    Last_Time = netWorkState;
                    NOWORKTIME = time;
                }
            }
        }
    }
    //获取系统时间
     public long getTime(){
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
         String format = simpleDateFormat.format(new Date());
         return Long.valueOf(format);
     }
}
